<?php
/**
 * User: fu
 * Date: 12-4-6
 * Time: 下午4:18
 */
require 'lib/lib.php';
include 'islogin.php';

$inputName = 'filedata';
$filepath = UPLOAD .'/'.date('Y-m-d');
$filename = $filepath . '/' . time();

if (!is_dir($filepath)){
    mkdir($filepath);
}



if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5上传
    $localName=urldecode($info[2]);
    $path_parts = pathinfo($localName);
    $extname=$path_parts['extension'];
    $filename=$filename.'.'.$extname;
    file_put_contents($filename,file_get_contents("php://input"));
    echo sprintf('{"err":"","msg":"%s"}',str_replace(DOCROOT,'',$filename)) ;

}
else{//标准表单式上传
    if (isset($_FILES[$inputName])) {
        $newfile = new File($_FILES[$inputName]);
        if ($newfile->checkfile('img')) {
            if ($path = $newfile->save($filepath.'/')) {
                $filename = $path;
                echo sprintf('{"err":"","msg":"%s"}',str_replace(DOCROOT,'',$filename)) ;
            }else{
                $err=$newfile->errorinfo;
            }
        }else{
            $err=$newfile->errorinfo;

        }
    }

}